package pers.ijfxu.testngext.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;

/**
 * 文件工具类
 * 
 * @author ijfxu
 * @since 2022.4.27
 */
public class FileUtils {
    /**
     * 获取BufferedReader,默认读取编码格式为"UTF-8"
     * 
     * @param file 文件相对路径或绝对路径
     * @return BufferedReader
     * @throws java.io.FileNotFoundException 未发现文件
     * @author ijfxu  
     * @since 2022.4.27
     */
    public static BufferedReader getReader(String file) throws FileNotFoundException {
        BufferedReader reader = null;
        try {
            reader = getReader(file, "UTF-8");
        } catch (Exception e) {
            System.err.println("UnsupportedEncodingException");
        }
        return reader;
    }

    /**
     * 获取BufferedReader
     * @param file  文件相对路径或绝对路径
     * @param charsetName 读取文件内容的编码格式
     * @return java.io.BufferedReader
     * @throws java.io.UnsupportedEncodingException 编码不支持
     * @throws java.io.FileNotFoundException 文件没有找到
     * @author ijfxu  
     * @since 2022.4.27
     */
    public static BufferedReader getReader(String file, String charsetName)
            throws UnsupportedEncodingException, FileNotFoundException {
        return new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
    }
}
